import router from "./router";
import store from "./store";
// import { ElMessage } from "element-plus";
import NProgress from "nprogress"; // 进度条
import "nprogress/nprogress.css"; // 进度条样式

NProgress.configure({ showSpinner: false });

const whiteList = ["/login", "/auth-redirect"];

router.beforeEach(async(to, from, next) => {
    NProgress.start();
    const hasToken = store.getters.token;

    if (hasToken) {
        if (to.path === "/login") {
            next({ path: "/" });
            NProgress.done();
        } else {
            next();
            // const hasRoutes = store.getters.permisstionRoutes;
            // if (hasRoutes.length == 0) {
            //     try {
            //         await store.dispatch("user/GenerateRoutes");
            //         next({...to, replace: true });
            //     } catch (error) {
            //         ElMessage.error(error || "Has Error");
            //         next("/login");
            //     }
            // } else {
            //     next();
            // }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next();
        } else {
            next("/login");
            NProgress.done();
        }
    }
});

router.afterEach(() => {
    NProgress.done();
});